﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Security.Entity;

namespace Pixysoft.Framework.Security
{
    [Serializable]
    public class UserInfo
    {
        string token = null;//存储在内存的标识符
        bool isAnonymous = true;
        bool isSuperUser = false;

        string name = "Guest";
        List<string> roleList = new List<string>();
        List<string> pageRoleList = new List<string>();
        List<string> pageResList = new List<string>();

        public UserInfo()
        {
        }

        internal UserInfo(string loginName, string token)
        {
            this.token = token;
            name = loginName;
            roleList = new List<string>();
            //groupList = new List<string>();
            pageRoleList = new List<string>();
            pageResList = new List<string>();
        }

        internal void Initialize(User value)
        {
            pageResList.Clear();

            roleList.Clear();

            pageRoleList.Clear();


            if (value == null)
                return;


            //09-12-23 提高性能

            foreach (PageRole role in value.PageRoleList)
            {
                pageRoleList.Add(role.RoleName);

                foreach (string res in role.PageResList)
                {
                    if (!pageResList.Contains(res))
                        pageResList.Add(res);
                }
            }

            foreach (Role role in value.RoleList)
            {
                roleList.Add(role.RoleName);
            }

            isSuperUser = value.IsSuperUser;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public List<string> RoleList
        {
            get { return roleList; }
            set { roleList = value; }
        }
        public List<string> PageRoleList
        {
            get { return pageRoleList; }
            set { pageRoleList = value; }
        }
        public List<string> PageResList
        {
            get { return pageResList; }
            set { pageResList = value; }
        }

        /// <summary>
        /// 是否为匿名用户
        /// </summary>
        public bool IsAnonymous
        {
            get { return this.isAnonymous; }
            set { isAnonymous = value; }
        }
        /// <summary>
        /// 是否超级用户
        /// </summary>
        public bool IsSuperUser
        {
            get { return isSuperUser; }
            set { isSuperUser = value; }
        }

        /// <summary>
        /// 获取用户的Token
        /// </summary>
        internal string Token
        {
            get { return this.token; }
            set { this.token = value; }
        }
    }
}
